package com.zhan.rkforum.service;


import com.zhan.rkforum.util.RedisKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.stereotype.Service;

@Service
@SuppressWarnings("unchecked")
public class FollowServiceImpl implements FollowService{


    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    @Override
    public void follow(int entityType, int entityId, int userId) {
        redisTemplate.execute(new SessionCallback<Object>() {
            @Override
            public Object execute(RedisOperations redisOperations) throws DataAccessException {
                String followeeKey = RedisKeyUtil.getFolloweeKey(entityType, userId);
                String followerKey = RedisKeyUtil.getFollowerKey(entityType, entityId);
                redisOperations.multi();

                redisOperations.opsForZSet().add(followeeKey, String.valueOf(entityId), System.currentTimeMillis() * 1.0);//关注实体
                redisOperations.opsForZSet().add(followerKey, String.valueOf(userId), System.currentTimeMillis() * 1.0);//关注用户

                return redisOperations.exec();
            }
        });
    }

    @Override
    public void unfollow(int entityType, int entityId, int userId) {
        redisTemplate.execute(new SessionCallback<Object>() {
            @Override
            public Object execute(RedisOperations redisOperations) throws DataAccessException {
                String followeeKey = RedisKeyUtil.getFolloweeKey(entityType, userId);
                String followerKey = RedisKeyUtil.getFollowerKey(entityType, entityId);
                redisOperations.multi();

                redisOperations.opsForZSet().remove(followeeKey, String.valueOf(entityId));//取关实体
                redisOperations.opsForZSet().remove(followerKey, String.valueOf(userId));//取关用户

                return redisOperations.exec();
            }
        });
    }

    /**
     * 查询关注实体的数量
     *
     * @param entityType 实体类型
     * @param userId     用户id
     * @return {@link Long}
     */
    @Override
    public Long getFolloweeCount(int entityType, int userId) {
        String followeeKey = RedisKeyUtil.getFolloweeKey(entityType, userId);
        return redisTemplate.opsForZSet().zCard(followeeKey);
    }

    /**
     * 查询粉丝计数
     *
     * @param entityType 实体类型
     * @param entityId   实体的id
     * @return {@link Long}
     */
    @Override
    public Long getFollowerCount(int entityType, int entityId) {
        String followerKey = RedisKeyUtil.getFollowerKey(entityType, entityId);
        return redisTemplate.opsForZSet().zCard(followerKey);
    }

    @Override
    public boolean hasFollowed(int entityType, int entityId, int userId) {
        String followeeKey = RedisKeyUtil.getFolloweeKey(entityType, userId);
        return redisTemplate.opsForZSet().score(followeeKey,String.valueOf(entityId)) != null;
    }
}
